<?php

use yii\db\Migration;

/**
 * Class m181119_062928_init_auth_manager
 */
class m181119_062928_init_auth_manager extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $auth = Yii::$app->authManager;

        // 创建角色
        $ceo = $auth->createRole('ceo');
        $auth->add($ceo);
        $hr = $auth->createRole('hr');
        $auth->add($hr);
        $pm = $auth->createRole('pm');
        $auth->add($pm);
        $sales = $auth->createRole('sales');
        $auth->add($sales);

        // 建立角色等级关系
        $auth->addChild($ceo, $hr);
        $auth->addChild($ceo, $pm);
        $auth->addChild($ceo, $sales);

        // 建立三组权限
        $hrPower = $auth->createPermission('hrPower');
        $hrPower->description = 'hr power';
        $auth->add($hrPower);
        $pmPower = $auth->createPermission('pmPower');
        $pmPower->description = 'pm power';
        $auth->add($pmPower);
        $salesPower = $auth->createPermission('salesPower');
        $salesPower->description = 'sales power';
        $auth->add($salesPower);

        // 将权限分配给角色
        $auth->addChild($hr, $hrPower);
        $auth->addChild($pm, $pmPower);
        $auth->addChild($sales, $salesPower);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $auth = Yii::$app->authManager;
        $auth->removeAll();
        echo "m181119_062928_init_auth_manager safe down.\n";
    }
}
